package com.ebei.admin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ebei.admin.common.constant.Constants;
import com.ebei.admin.common.util.CommonUtil;
import com.ebei.admin.model.dto.UserDTO;
import com.ebei.admin.model.dto.UserInfo;
import com.ebei.admin.model.entity.AppVersion;
import com.ebei.admin.model.entity.SysUser;
import com.ebei.admin.service.AppVersionService;
import com.ebei.admin.service.SysRoleService;
import com.ebei.admin.service.SysUserService;
import com.ebei.common.constant.CommonConstant;
import com.ebei.common.util.Result;
import com.ebei.common.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/login")
@Api(value = "登陆", description = "登陆接口")
public class LoginController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private AppVersionService appVersionService;

    @ApiOperation("登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userAccount", value = "用户账号", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true)
    })
    @PostMapping("/doLogin")
    public Result<UserInfo> login(@RequestBody UserDTO userDTO, HttpServletRequest request) {
        SysUser condition = new SysUser();
        condition.setUserAccount(userDTO.getUserAccount().trim());
        condition.setDelFlag(CommonConstant.STATUS_NORMAL);
        SysUser loginUser = sysUserService.selectOne(new EntityWrapper<>(condition));

        if (loginUser == null) {
            return new Result<>(null, Result.FAIL, "用户不存在");
        }

//        if (!CommonUtil.ENCODER.matches(userDTO.getPassword(), loginUser.getPassword())) {
//            return new Result<>(null, Result.FAIL, "密码错误");
//        }

        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userDTO, userVO);
        UserInfo userInfo = sysUserService.findUserInfo(userVO, Boolean.TRUE);

        if (userInfo != null && userInfo.getSysUser() != null) {
            // 最后更新时间
            loginUser.setLastLogin(new Date());
            sysUserService.updateById(loginUser);
            HttpSession session = request.getSession(true);
            session.setAttribute(Constants.LOGIN_INFO, userInfo);
            session.setMaxInactiveInterval(Constants.SESSION_TIMEOUT);//单位为秒,,不是毫秒,,,这里是30分钟
        }

        return new Result<>(userInfo, Result.SUCCESS, "登陆成功");

    }

    @ApiOperation(value = "登陆app")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userAccount", value = "用户账号", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true)
    })
    @PostMapping("/doAppLogin")
    public Result<UserInfo> appLogin(@RequestBody UserDTO userDTO) {
        SysUser condition = new SysUser();
        condition.setUserAccount(userDTO.getUserAccount().trim());
        condition.setDelFlag(CommonConstant.STATUS_NORMAL);
        SysUser loginUser = sysUserService.selectOne(new EntityWrapper<>(condition));

        if (loginUser == null) {
            return new Result<>(null, Result.FAIL, "用户不存在");
        }

        if (!CommonUtil.ENCODER.matches(userDTO.getPassword(), loginUser.getPassword())) {
            return new Result<>(null, Result.FAIL, "密码错误");
        }

        // 超管角色限制登陆
        if(sysRoleService.hasPermission(loginUser.getUserId(),Constants.SUPER_ADMIN)){
            return new Result<>(null, Result.FAIL, "超级管理员限制登陆app");
        }

        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userDTO, userVO);
        UserInfo userInfo = sysUserService.findUserInfo(userVO, Boolean.FALSE);

        if (userInfo != null && userInfo.getSysUser() != null) {
            // 最后更新时间
            loginUser.setLastLogin(new Date());
            sysUserService.updateById(loginUser);
        }

        return new Result<>(userInfo, Result.SUCCESS, "登陆成功");
    }

    @ApiOperation(value = "退出登陆")
    @RequestMapping("/logout")
    public Result logout(HttpServletRequest request) {
        request.getSession(true).invalidate();
        return Result.OK;
    }

    @ApiOperation("检查更新")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "appCode", value = "app编码", required = true),
        @ApiImplicitParam(name = "appType", value = "app类型", required = true)
    })
    @GetMapping("/checkVersion")
    public Result checkVersion(@RequestParam String appCode,Integer appType){

        AppVersion condition = new AppVersion();
        condition.setAppCode(appCode);
        condition.setAppType(appType);
        AppVersion appVersion;
        try {
            appVersion = appVersionService.selectOne(new EntityWrapper<>());
        } catch (Exception e){
            return new Result(Result.FAIL,"数据错误");
        }

        return new Result(appVersion,Result.SUCCESS,"获取成功");
    }

}
